#!/bin/bash

IFS_BAK=$IFS
IFS="
"

inv="/home/mac/inv/test2"
movies="/home/mac/Movies"
tagalog="/home/mac/TagalogMovie"
show="/home/mac/TvShow"



/bin/ls -l $movies | /usr/bin/cut -d ":" -f 2- | /usr/bin/cut -d " " -f 2- > $inv/invmovies.txt
/bin/ls -l $tagalog | /usr/bin/cut -d ":" -f 2- | /usr/bin/cut -d " " -f 2- > $inv/invtagalog.txt
/usr/bin/find $show -iregex '.*\(nfo\)'|/bin/sed 's/.*/"&"/' > $inv/invshow-nfofiles.txt
/usr/bin/find $show -iregex '.*\(nfo\)' > $inv/invshow-nfofiles.txt
/bin/cat $inv/invshow-nfofiles.txt|/bin/grep ' - '|/usr/bin/cut -d "/" -f 5|/usr/bin/sort -u|/bin/sed 's/.*/"&"/' > $inv/invshow.txt

/bin/sed -i '1d' $inv/invmovies.txt
/bin/sed -i '1d' $inv/invtagalog.txt

echo " "
echo "**********************************************************************"
echo "Generating Movies Inventory from Meta Data - `date`"
echo "**********************************************************************"
echo "Title;Year;Release Date;Studio;Director;Rating;Duration (sec);Height;Width;Audio Channel;Audio Codec;Video Codec;File Extention;File Size(kb)" > $inv/invmovies.csv

for fn in `/bin/cat $inv/invmovies.txt`; do
	xy="`/usr/bin/xml_grep title --text_only $movies/$fn/$fn.nfo` - `date`"
	echo "$xy"
	x="`/usr/bin/xml_grep title --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep year --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep releasedate --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep studio --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep director --text_only $movies/$fn/$fn.nfo|/bin/sed 'N;s/\n/ | /'`;`/usr/bin/xml_grep rating --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep durationinseconds --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep height --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep width --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep channels --text_only $movies/$fn/$fn.nfo`;`/usr/bin/xml_grep codec --text_only $movies/$fn/$fn.nfo|/bin/sed 'N;s/\n/;/'`;"
	z=`/usr/bin/du -l $movies/$fn/* | /usr/bin/sort -n -r  | /usr/bin/head -1 |/usr/bin/cut -d '/' -f 1`
	y=`/usr/bin/du -l $movies/$fn/* | /usr/bin/sort -n -r | /usr/bin/head -1 |/usr/bin/cut -d '/' -f 2-|/bin/sed 's/.*\(...\)$/\1/'`
	echo "$x$y;$z" >> $inv/invmovies.csv 
done 

echo "Generating TagalogMovies Inventory from Meta Data - `date`"
echo "Title,year,Release Date,Studio,Director,Rating,Duration (sec),Height,Width,Audio Channel,Audio Codec,Video Codec,File Extention,File Size(kb)" > $inv/invtagalog.csv

for fn in `/bin/cat $inv/invtagalog.txt`; do
        xy="`/usr/bin/xml_grep title --text_only $tagalog/$fn/$fn.nfo` - `date`"
	echo "$xy"
	x="`/usr/bin/xml_grep title --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep year --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep releasedate --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep studio --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep director --text_only $tagalog/$fn/$fn.nfo|/bin/sed 'N;s/\n/ | /'`,`/usr/bin/xml_grep rating --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep durationinseconds --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep height --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep width --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep channels --text_only $tagalog/$fn/$fn.nfo`,`/usr/bin/xml_grep codec --text_only $tagalog/$fn/$fn.nfo|/bin/sed 'N;s/\n/,/'`,"
        z=`/usr/bin/du -l $tagalog/$fn/* | /usr/bin/sort -n -r  | /usr/bin/head -1 |/usr/bin/cut -d '/' -f 1`
        y=`/usr/bin/du -l $tagalog/$fn/* | /usr/bin/sort -n -r | /usr/bin/head -1 |/usr/bin/cut -d '/' -f 2-|/bin/sed 's/.*\(...\)$/\1/'`
        echo "$x$y,$z" >> $inv/invtagalog.csv
done

echo "Generating TvShow Inventory from Meta Data - `date`"
echo "Show Title,Studio,Show Rating,Premiered Date,Episode Title,Aired Date,Episode Rating,Season ID,Episode ID,Duration,Height,Width,Audio Channels,Audio Codec,Video Codec,File Extention,File Size(kb)" > $inv/invshow.csv

for fns in `/bin/cat $inv/invshow-nfofiles.txt|/bin/grep ' - '`; do
	a="`echo $fns|/usr/bin/cut -d '/' -f 5`"
	b="`/usr/bin/xml_grep studio --text_only $show/$a/tvshow.nfo`,`/usr/bin/xml_grep rating --text_only $show/$a/tvshow.nfo`,`/usr/bin/xml_grep premiered --text_only $show/$a/tvshow.nfo`"
	aa="`echo $fns |/usr/bin/cut -d '/' -f 6-| /bin/sed 's/ /\\ /g'`"
	aaa="`echo $fns |/usr/bin/cut -d '/' -f 6-|/bin/sed 's/.\{3\}$/*/'| /bin/sed 's/ /\\ /g'`"
	c="`/usr/bin/xml_grep title --text_only $show/$a/$aa`,`/usr/bin/xml_grep aired --text_only $show/$a/$aa`,`/usr/bin/xml_grep rating --text_only $show/$a/$aa`,`/usr/bin/xml_grep season --text_only $show/$a/$aa`,`/usr/bin/xml_grep episode --text_only $show/$a/$aa`,`/usr/bin/xml_grep durationinseconds --text_only $show/$a/$aa`,`/usr/bin/xml_grep height --text_only $show/$a/$aa`,`/usr/bin/xml_grep width --text_only $show/$a/$aa`,`/usr/bin/xml_grep channels --text_only $show/$a/$aa`,`/usr/bin/xml_grep codec --text_only $show/$a/$aa|/bin/sed 'N;s/\n/,/'`"
	e="`/usr/bin/du -l $show/$a/$aaa| /usr/bin/sort -n -r|/usr/bin/head -1 |/usr/bin/cut -d '/' -f 1`"
	d="`/usr/bin/du -l $show/$a/$aaa| /usr/bin/sort -n -r| /usr/bin/head -1 |/usr/bin/cut -d '/' -f 2-|/bin/sed 's/.*\(...\)$/\1/'`"
	echo "$a - `/usr/bin/xml_grep title --text_only $show/$a/$aa` - `date`"
	echo "$a,$b,$c,$d,$e" >> $inv/invshow.csv
done
echo "DONE Generating - `date`"
IFS=$IFS_BAK
IFS_BAK=
